<div id="content-header">
    <h1><?php echo $this->request->data['Node']['title'];?></h1>
</div>
<div id="breadcrumb">
    <a href="<?php echo $this->Html->url('/admin/')?>" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> <?php echo __d('system','Home');?></a>
    <a href="#"><?php echo __d('system','Edit');?></a>
    <a href="#" class="current"><?php echo $datas['Node']['title']?></a>
</div>
<div class="container-fluid">

<div class="row-fluid">
    <div class="span12">
        <?php
            echo $this->Form->create('Node', array('id' => 'paginate','class'=>'form-horizontal', 'action' => 'edit', 'url' => $this->request->here(false), 'type' => 'file',
                    'inputDefaults' => array('label' => false, 'format' => array('before', 'label', 'between', 'input', 'after', 'error'))));

            echo $this->Html->script('ckeditor/ckeditor');
            echo $this->element('toolbar');

        ?>	
        <div class="widget-box">
            <?php            
                $fieldsets['term_id'] = $this->Form->input('term_id', array(            
                        'type' => 'select',
                        'options' => $listTerms,
                        'value' => $nodeTerm['NodeTerm']['term_id'],
                        'empty' => '-- None --',
                        'label' => array('class' => 'control-label'),
                        'between' => '<div class="controls">',
                        'div' => array('class' => 'control-group'),
                        'after' => '</div>',
                        'escape' => false));  

                $fieldsets['user_id'] = $this->Form->input('user_id', array(
                        'label' => array('class' => 'control-label'),
                        'between' => '<div class="controls">',
                        'div' => array('class' => 'control-group'),
                        'after' => '</div>',
                        'type' => 'hidden'));
            ?>
            <?php 
                $fieldsets['title'] = $this->Form->input('title', array(
                        'label' => array('text'=> __d('system','Title'),'class' => 'control-label'),
                        'between' => '<div class="controls">',
                        'div' => array('class' => 'control-group'),
                        'after' => '</div>',
                        'type' => 'text'));                               
                $fieldsets['node_term_id'] = $this->Form->input('node_term_id', array(
                        'label' => array('class' => 'control-label'),
                        'between' => '<div class="controls">',
                        'div' => array('class' => 'control-group'),
                        'after' => '</div>',
                        'value' => $nodeTerm['NodeTerm']['id'],
                        'type' => 'hidden'));              
                $fieldsets['is_published'] = $this->Form->input('is_published', array(
                        'label' => array('text'=>__d('system','Show/Hide'),'class' => 'control-label'),
                        'between' => '<div class="controls">',
                        'div' => array('class' => 'control-group'),
                        'after' => '</div>',
                        'type' => 'checkbox'));  


            ?>
            <?php
                $layout = array(
                    array('node_term_id', 'user_id'),
                    array('title','term_id'),        
                    array('is_published'),
                );
                foreach ($fields as $field) {
                    $type = 'text';
                    if ($field['Field']['field_type'] == 'img' || $field['Field']['field_type'] == 'file') {
                        $type = 'file';
                    }
                    if($field['Field']['field_type'] == 'textarea'){
                        $type= 'textarea';
                    }
                    else if($field['Field']['field_type'] == 'checkbox'){
                        $type= 'checkbox';
                    }
                    $chosseckeditor = null;

                    switch ($field['Field']['use_wyswyg']) {
                        case null:
                            $chosseckeditor =null;
                            break;
                        case 0:
                            $chosseckeditor =null;
                            break;                
                        case 1:
                            $chosseckeditor = 'ckeditor';
                            break;
                    } 
                    $fieldsets['field_' . $field['Field']['name']] = $this->Form->input('field_' . $field['Field']['name'], array(
                            'label' => array('text'=>$field['Field']['label'],'class' => 'control-label'),
                            'between' => '<div class="controls">',
                            'div' => array('class' => 'control-group'),
                            'after' => '</div>',
                            'class'=> $chosseckeditor,
                            'type' => $type));
                    $layout[] = array('field_' . $field['Field']['name']);
                }

                $layout = array(
                    array(
                        'Basic setting',
                        $layout,
                    )
                );
                echo $this->Layout->form($layout, $fieldsets);
            ?>
            <?php if(isset($images) && !empty($images)){?>
                <div class="control-group add-more">
                    <label class="control-label" for="NodeFieldImage">Image</label>
                    <div class="controls">
                        <?php echo $this->Form->input('ProImage', array(
                                    'class'=>'file-upload',
                                    'label' => false,
                                    'type'  => 'file', 
                                    'name' => 'data[ProImage][]'));?>

                    </div>

                </div>	
                <div id="cloneImage"></div>
                <a style="padding-left: 10px; clear: both; display: block;" href="javascript:add_more();"><?php echo __d("system", "Add more image")?></a>                                                   
                <?php }?>   
            <?php if(!empty($galleries)){?>
                <div class="widget-box">
                    <div class="widget-title">
                        <span class="icon">
                            <i class="icon-picture"></i>									
                        </span>
                        <h5>List of Images</h5>
                    </div>
                    <div class="widget-content">
                        <div class="row-fluid row-fluid-two">
                            <ul class="thumbnails">
                                <?php if(!empty($galleries)){?>
                                    <?php foreach($galleries as $key_image=>$image){


                                        ?>
                                        <li class="span3">
                                            <div class="thumbnail">
                                                <?php echo $this->Html->image('/img/'.$image, array('width'=>'287', 'height'=>'137', 'alt'=> 'Image')) ?>
                                                <div class="wd-delete-edit">
                                                    <a href="<?php echo $this->Html->url(array('plugin'=>'node','controller' => 'node', 'action' => 'edit_image',$typeId, $datas['Node']['id'],$key_image));?>"><i class="icon-pencil icon-white">&nbsp;</i> Edit</a>
                                                    <a id="image_del_<?php echo $key_image; ?>" href="javascript:void(0);" onclick="delete_image('<?php echo $key_image?>');"><i class="icon-remove icon-white">&nbsp;</i> Delete</a>
                                                </div>
                                            </div>
                                        </li>
                                        <?php }?> 
                                    <?php } ?> 
                            </ul>
                        </div>
                    </div>
                </div>		
                <?php }?>
                <?php if($typeId==News):?>
                    <div class="wd-group-user">
                    <div class="wd-email">
                        <div class="wd-input">
                            <label><span> <?php echo __d('system','Choose Group');?>:</span></label>
                            <?php echo $this->Form->input('groupUser',array(
                                        'type'=>'select',
                                        'id'=>'PaginateGroupUser',
                                        'div'=>false,
                                        'label'=>false,
                                        'options'=>$groupUser
                                    ));
                            ?>
                        </div>
                    </div>                     
                    <label><span><?php echo __d('system','Danh sách email');?>:</span></label>
                    <?php if(!empty($list_email)):?>
                        <ul class="list-user-email">
                            <li class="check_all">                            
                                <input type="checkbox" value="" class="input-chk-all"><?php echo __d('system','Check All');?>                                         
                            </li> 
                            <?php foreach($list_email as $k=>$v):?>                       
                                <li class="mail_<?php echo $v['Node']['id'];?>">
                                    <input type="checkbox" onclick="add_mail_user('<?php echo $v['Node']['field_email'];?>','<?php echo $v['Node']['id'];?>');" value="<?php echo $v['Node']['field_email'];?>" class="_lst_email_<?php echo $v['Node']['id'];?>"> <?php echo $v['Node']['field_email'];?>                                        
                                </li>                                
                                <?php endforeach;?>
                        </ul>
                        <?php else:?>
                        <?php echo __d('system','Không có email nào !!!');?>
                        <?php endif;?>                           
                </div>        
                    <div class="wd-group-checked">
                        <label><span><?php echo __d('system','List Checked');?></span></label>                               
                        <ul class="list-email-checked"></ul>
                    </div>
                <?php endif;?>
        </div>
        <?php
            echo $this->Form->hidden('node_type_id');
            echo $this->Form->hidden('Paginate.task', array('id' => 'task', 'value' => ''));
            echo $this->Form->end();
        ?>        									
    </div>	
</div>
<script >
    $('document').ready(function() {            
            $('.check_all .input-chk-all').live('click',function(){                
                    if($(this).is(':checked')){                        
                        myItems = '<?php echo json_encode($list_email);?>';                    
                        var obj = jQuery.parseJSON(myItems);
                        var lenstr = obj.length;
                        for(objs in obj){
                            var _getId = parseInt(obj[objs]['Node']['id']) ;  
                            var _getEmail = String(obj[objs]['Node']['field_email']) ;                          
                            var _email = '';
                            _email += "<li class=mail_"+_getId+">";
                            _email += "<input type=hidden"; 
                            _email += " name=data[Node][email][]";       
                            _email += " value="+_getEmail;
                            _email += " class=_lst_email_"+_getId;
                            _email += "> ";
                            _email += _getEmail;
                            _email += "<a href='#' onclick=_delete_checked_mail('"+_getId+"')>";
                            _email += " Delete";
                            _email += "</a>";
                            _email += "</li>";
                            $('.list-email-checked').append(_email); 
                            $('.list-user-email .mail_'+_getId+' span').addClass('checked');                          
                        }                                                    
                    }else{
                      $('.list-email-checked li').remove();
                      $('.list-user-email li div span').removeClass('checked'); 
                    }                
            })                                                
    });     
</script>
<script type="text/javascript">
    jQuery(document).ready(function($){
            $('<span>Size: 1600x850</span>').insertAfter("#NodeFieldBackgroundImage");
            $('<span>Enter a valid number: (int)</span>').insertAfter("#NodeFieldBackgroundPiority");    
    });
    function add_more(){
        var element= jQuery("#cloneImage").prev();
        jQuery(element).clone().insertBefore("#cloneImage");
        var $this = Green.Elfinder;
        jQuery('form input.file-upload').prop('readonly' , true).click(function(){
                $this.show(jQuery(this));
        });
    }
    function delete_image(id){
        if (confirm("Are you sure you want to delete?")) {
            var href= "<?php echo '/node/node/delete_image/'?>"+id;
            jQuery.ajax({
                    url: href,
                    type: "GET",
                    success: function(data) {
                        jQuery('#image_'+id).remove();
                        jQuery('#image_del_'+id).remove();
                        window.location.reload();
                        //alert('Load was performed.');
                    }
            });
        }
    }
</script>
<style type="text/css">
    .wd-group-user,.wd-group-checked{border:none;border-bottom: 1px solid #ccc;border-top: 1px solid #ccc;padding-top: 0px;}
</style>